package cn.iocoder.yudao.module.mybusiness.service.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.mybusiness.domain.BussinessMod;
import cn.iocoder.yudao.module.mybusiness.domain.Facility;
import cn.iocoder.yudao.module.mybusiness.dto.DeptFcfTree;
import cn.iocoder.yudao.module.mybusiness.dto.Fcf;
import cn.iocoder.yudao.module.mybusiness.mapper.FacilityMapper;
import cn.iocoder.yudao.module.mybusiness.param.FacilityParam;
import cn.iocoder.yudao.module.mybusiness.service.FcfService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class FcfServiceImpl extends ServiceImpl<FacilityMapper, Facility> implements FcfService {

    @Resource
    private FacilityMapper facilityMapper;


    @Resource
    private DeptApi deptApi;

    public PageResult<Facility> getList(FacilityParam facilityParam) {
        PageResult<Facility> pageResult = facilityMapper.selectPage(facilityParam, new LambdaQueryWrapperX<Facility>()
                .likeIfPresent(Facility::getFacName, facilityParam.getFacName())
                .eqIfPresent(Facility::getStatus,facilityParam.getStatus())
                .eq(Facility::getIsDelete,0)
                .orderByDesc(Facility::getId));
        pageResult.getList().forEach(facility -> {
            DeptRespDTO dept = deptApi.getDept(facility.getDept());
            facility.setDeptName(dept.getName());
        });
        return pageResult;
    }

    @Override
    public void create(Facility facility) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        facility.setUserId(loginUser.getId());
        SimpleDateFormat format = new SimpleDateFormat(DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
        facility.setCreateTime(format.format(new Date()));
        facilityMapper.insert(facility);
    }

    @Override
    public void delete(Long id) throws Exception {
        Facility facility = facilityMapper.selectById(id);
        if(facility.getStatus()!=1){
            throw new Exception("设备繁忙");
        }
        facility.setIsDelete(1);
        facilityMapper.updateById(facility);
    }

    @Override
    public List getDeptFcfTree() {
        List<Facility> facilities = facilityMapper.selectList(new LambdaQueryWrapperX<Facility>()
                .eq(Facility::getIsDelete, 0)
                .eq(Facility::getStatus, 1));

        List<Long> collect = facilities.stream().map(Facility::getDept).collect(Collectors.toList());
/*        List<DeptRespDTO> deptList = deptApi.getDeptList(collect);
        Map<String, DeptFcfTree> map = new HashMap();
        deptList.forEach(deptRespDTO -> {
            facilities.forEach(facility -> {
               if (facility.getDept()==deptRespDTO.getId()){
                   Fcf fcf = new Fcf(facility.getId(),facility.getFacName());
                   if(map.containsKey(String.valueOf(deptRespDTO.getId()))){
                       DeptFcfTree deptFcfTree = map.get(String.valueOf(deptRespDTO.getId()));
                       deptFcfTree.getChildren().add(fcf);
                   }
               }
            });
        });*/
        Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(collect);
        Map<Long, DeptFcfTree> map = new HashMap();
        facilities.forEach(facility -> {
            Fcf fcf = new Fcf(facility.getId(),facility.getFacName());
            DeptRespDTO deptRespDTO = deptMap.get(facility.getDept());
            DeptFcfTree deptFcfTree;
            if(map.containsKey(facility.getDept())){
                 deptFcfTree = map.get(facility.getDept());
            }else {
                 deptFcfTree = new DeptFcfTree();
                deptFcfTree.setLabel(deptRespDTO.getName());
                deptFcfTree.setValue(deptRespDTO.getId());
                deptFcfTree.setChildren(new ArrayList());
                map.put(deptRespDTO.getId(),deptFcfTree);
            }
            deptFcfTree.getChildren().add(fcf);

        });
        List<DeptFcfTree> collect1 = map.values().stream().collect(Collectors.toList());
        return collect1;
    }
}
